package com.dailyblue.java.project.smbms.shopping.controller;

import com.dailyblue.java.commons.util.JsonResult;
import com.dailyblue.java.project.smbms.shopping.service.ShoppingService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/cart")
@CrossOrigin
public class ShoppingController {
    @Resource
    private ShoppingService shoppingService;

    @GetMapping("/{id}/{num}")
    public JsonResult add(HttpServletRequest request, @PathVariable("id") Integer goodsId, @PathVariable("num") Integer num) {
        return shoppingService.addCart(request.getHeader("token"), goodsId, num);
    }

    @GetMapping
    public JsonResult find(HttpServletRequest request) {
        return shoppingService.showCart(request.getHeader("token"));
    }

    @DeleteMapping("/{id}")
    public JsonResult remove(HttpServletRequest request, @PathVariable("id") int goodsId) {
        shoppingService.removeGoods(request.getHeader("token"), goodsId);
        return shoppingService.showCart(request.getHeader("token"));
    }

    @GetMapping("/update/{id}/{num}")
    public JsonResult update(HttpServletRequest request, @PathVariable("id") int goodsId, @PathVariable("num") int num) {
        shoppingService.updateCart(request.getHeader("token"), goodsId, num);
        return shoppingService.showCart(request.getHeader("token"));
    }
}
